package edu.gatech.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import edu.gatech.datastorage.TreatmentRecord;
import edu.gatech.presenters.TreatmentRecordPresenter;
import edu.gatech.userclasses.Doctor;
import edu.gatech.userclasses.Patient;
import edu.gatech.userclasses.User;
import edu.gatech.utilities.Persistence;

public class TreatmentRecordPanel {
	static String[] months = {"January", "February", "March", "April",
		"May", "June", "July", "August", "September", "October", 
		"November", "December"};
	private static JTextField textField;
	private static JComboBox nurses;
	private static JComboBox doctors;
	private static JTextField textField_3;
	private static JTextField textField_4;
	private static JTextField textField_5;
	private static JTextField textField_6;
	private static JTextField textField_7;
	private static Patient currentPatient;
	private static JRadioButton radioButton;
	private static JRadioButton radioButton_1;
	private static ButtonGroup group;
	private static JTextArea textArea;
	private static JTextArea textArea_1;
	private static User currentUser;
	private static JFrame records;
	public TreatmentRecordPanel(Patient cur, User curUser){
		currentPatient = cur;
		currentUser = curUser;
		TreatmentRecord record = currentPatient.getInfo().getLastRecord();
		String[] date = record.getAppointment().getDate().split(" ");
		String[] time = record.getAppointment().getTime().split(" ");
		ArrayList<String> nurseList = new ArrayList<String>();
		for (User u : Persistence.getUserList()){
			if (u.getUserType().equals("Nurse")){
				nurseList.add(u.getName());
			}
		}
		String[] nurseArray = new String[nurseList.size()];
		for(int i = 0; i < nurseList.size(); i++){
			nurseArray[i] = nurseList.get(i);
		}
		records = new JFrame("Treatment Records");
		JPanel recordPanel = new JPanel();
		recordPanel.setBounds(0, 0, 584, 442);
		
		MenuBar menuBar = new MenuBar(records);
		records.getContentPane().setLayout(null);
        
		records.getContentPane().add(recordPanel);
		recordPanel.setLayout(null);
		
		JLabel lblCurrentPatient = new JLabel("Current Patient:");
		lblCurrentPatient.setBounds(22, 40, 109, 14);
		recordPanel.add(lblCurrentPatient);
		
		JLabel label = new JLabel(currentPatient.getInfo().getName());
		label.setBounds(22, 61, 298, 25);
		label.setFont(new Font("Arial", Font.BOLD, 14));
		recordPanel.add(label);
		
		JLabel lblTreatingDoctor = new JLabel("Doctor:");
		lblTreatingDoctor.setFont(new Font("Arial", Font.PLAIN, 10));
		lblTreatingDoctor.setBounds(21, 126, 47, 14);
		recordPanel.add(lblTreatingDoctor);
		
		JLabel lblMonth = new JLabel("Month:");
		lblMonth.setFont(new Font("Arial", Font.PLAIN, 10));
		lblMonth.setBounds(22, 163, 46, 14);
		recordPanel.add(lblMonth);
		
		JLabel lblDate = new JLabel("Day:");
		lblDate.setFont(new Font("Arial", Font.PLAIN, 10));
		lblDate.setBounds(21, 201, 46, 14);
		recordPanel.add(lblDate);
		
		JLabel lblTime = new JLabel("Time:");
		lblTime.setFont(new Font("Arial", Font.PLAIN, 10));
		lblTime.setBounds(21, 242, 46, 14);
		recordPanel.add(lblTime);
		
		textField = new JTextField();
		textField.setBounds(77, 235, 86, 20);
		textField.setText(time[0]);
		textField.setFont(new Font("Arial", Font.PLAIN, 10));
		textField.setColumns(10);
		recordPanel.add(textField);
		
		group = new ButtonGroup();
		radioButton = new JRadioButton("AM");
		if(time[1].equals("A.M")){
			radioButton.setSelected(true);
		}
		radioButton.setBounds(169, 222, 41, 23);
		radioButton.setFont(new Font("Arial", Font.PLAIN, 10));
		group.add(radioButton);
		recordPanel.add(radioButton);
		
		radioButton_1 = new JRadioButton("PM");
		radioButton_1.setBounds(169, 245, 41, 23);
		if(time[1].equals("P.M")){
			radioButton_1.setSelected(true);
		}
		radioButton_1.setFont(new Font("Arial", Font.PLAIN, 10));
		group.add(radioButton_1);
		recordPanel.add(radioButton_1);
		
		nurses = new JComboBox(nurseArray);
		nurses.setBounds(77, 277, 133, 20);
		nurses.setFont(new Font("Arial", Font.PLAIN, 10));
		recordPanel.add(nurses);
		
		JComboBox comboBox = new JComboBox(months);
		comboBox.setEditable(false);
		comboBox.setSelectedItem(date[0]);
		comboBox.setBounds(78, 160, 132, 21);
		comboBox.setFont(new Font("Arial", Font.PLAIN, 10));
		recordPanel.add(comboBox);
		
		ArrayList<String> list = new ArrayList<String>();
		for (User u : Persistence.getUserList()){
			if (u.getUserType().equals("Doctor")){
				list.add(u.getName());
			}
		}
		String[] array = new String[list.size()];
		for(int i = 0; i < list.size(); i++){
			array[i] = list.get(i);
		}
		doctors = new JComboBox(array);
		doctors.setFont(new Font("Arial", Font.PLAIN, 10));
		doctors.setBounds(78, 123, 132, 20);
		recordPanel.add(doctors);
		
		textField_3 = new JTextField();
		textField_3.setFont(new Font("Arial", Font.PLAIN, 10));
		textField_3.setText(date[1]);
		textField_3.setBounds(77, 198, 133, 20);
		recordPanel.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblNurse = new JLabel("Nurse:");
		lblNurse.setFont(new Font("Arial", Font.PLAIN, 10));
		lblNurse.setBounds(21, 279, 46, 14);
		recordPanel.add(lblNurse);
		
		JLabel label_1 = new JLabel("Symptoms:");
		label_1.setFont(new Font("Arial", Font.PLAIN, 10));
		label_1.setBounds(242, 201, 74, 14);
		recordPanel.add(label_1);
		
		textArea = new JTextArea();
		String symptoms = currentPatient.getInfo().getLastRecord().getSymptoms();
		if(symptoms != null){
			textArea.setText(symptoms);
		}
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 10));
		textArea.setBounds(324, 201, 227, 96);
		recordPanel.add(textArea);
		
		
		JLabel lblBloodPressure = new JLabel("Blood Pressure:");
		lblBloodPressure.setFont(new Font("Arial", Font.PLAIN, 10));
		lblBloodPressure.setBounds(242, 126, 86, 14);
		recordPanel.add(lblBloodPressure);
		
		JLabel lblTemperature = new JLabel("Temperature:");
		lblTemperature.setFont(new Font("Arial", Font.PLAIN, 10));
		lblTemperature.setBounds(242, 163, 74, 14);
		recordPanel.add(lblTemperature);
		
		JLabel lblHeightl = new JLabel("Height:");
		lblHeightl.setFont(new Font("Arial", Font.PLAIN, 10));
		lblHeightl.setBounds(420, 126, 46, 14);
		recordPanel.add(lblHeightl);
		
		JLabel lblWeight = new JLabel("Weight:");
		lblWeight.setFont(new Font("Arial", Font.PLAIN, 10));
		lblWeight.setBounds(420, 163, 46, 14);
		recordPanel.add(lblWeight);
		
		String bloodPreassure = null;
		String temperature = null;
		String height = null;
		String weight = null;
		if(record.getVitalSigns() != null){
			String[] vitals = record.getVitalSigns().split(" ");
			bloodPreassure = vitals[0];
			temperature = vitals[1];
			height = vitals[2];
			weight = vitals[3];
		}
		textField_4 = new JTextField();
		if(bloodPreassure != null){
			textField_4.setText(bloodPreassure);
		}
		textField_4.setFont(new Font("Arial", Font.PLAIN, 10));
		textField_4.setBounds(324, 123, 86, 20);
		recordPanel.add(textField_4);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		if(temperature != null){
			textField_5.setText(temperature);
		}
		textField_5.setFont(new Font("Arial", Font.PLAIN, 10));
		textField_5.setColumns(10);
		textField_5.setBounds(324, 160, 86, 20);
		recordPanel.add(textField_5);
		
		textField_6 = new JTextField();
		if(weight != null){
			textField_6.setText(weight);
		}
		textField_6.setFont(new Font("Arial", Font.PLAIN, 10));
		textField_6.setColumns(10);
		textField_6.setBounds(465, 160, 86, 20);
		recordPanel.add(textField_6);
		
		textField_7 = new JTextField();
		if(height != null){
			textField_7.setText(height);
		}
		textField_7.setFont(new Font("Arial", Font.PLAIN, 10));
		textField_7.setColumns(10);
		textField_7.setBounds(465, 123, 86, 20);
		recordPanel.add(textField_7);
		
		JLabel lblDiagnosis = new JLabel("Diagnosis:");
		lblDiagnosis.setFont(new Font("Arial", Font.PLAIN, 10));
		lblDiagnosis.setBounds(21, 311, 68, 14);
		recordPanel.add(lblDiagnosis);
		
		textArea_1 = new JTextArea();
		String diagnosis = currentPatient.getInfo().getLastRecord().getDiagnosis();
		if(diagnosis != null){
			textArea_1.setText(diagnosis);
		}
		textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 10));
		textArea_1.setBounds(77, 308, 474, 66);
		recordPanel.add(textArea_1);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TreatmentRecordPresenter presenter = new TreatmentRecordPresenter(currentPatient);
				presenter.addRecord((String)doctors.getSelectedItem(), (String)nurses.getSelectedItem(), textArea.getText(), 
				textField_4.getText(), textField_5.getText(), textField_7.getText(), textField_6.getText(), textArea_1.getText());
				records.hide();
			}
		});
		btnAdd.setFont(new Font("Arial", Font.PLAIN, 10));
		btnAdd.setBounds(363, 385, 86, 23);
		recordPanel.add(btnAdd);
		
		JButton btnNewButton = new JButton("Cancel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				records.hide();
			}
		});
		btnNewButton.setFont(new Font("Arial", Font.PLAIN, 10));
		btnNewButton.setBounds(465, 385, 86, 23);
		recordPanel.add(btnNewButton);
		
		JButton btnViewDoctorsOrders = new JButton("View Doctors Orders");
		btnViewDoctorsOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TreatmentRecordPresenter presenter = new TreatmentRecordPresenter(currentPatient);
				presenter.viewReports();
			}
		});
		btnViewDoctorsOrders.setFont(new Font("Arial", Font.PLAIN, 10));
		btnViewDoctorsOrders.setBounds(441, 40, 133, 23);
		recordPanel.add(btnViewDoctorsOrders);
		
		JButton btnAddDoctorsOrders = new JButton("Add Doctors Orders");
		btnAddDoctorsOrders.setVisible(false);
		if(currentUser.getUserType().equals("Doctor")){
			btnAddDoctorsOrders.setVisible(true);
		}
		btnAddDoctorsOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TreatmentRecordPresenter presenter = new TreatmentRecordPresenter(currentPatient);
				presenter.addDoctorOrder((Doctor) currentUser);
			}
		});
		btnAddDoctorsOrders.setFont(new Font("Arial", Font.PLAIN, 10));
		btnAddDoctorsOrders.setBounds(294, 11, 133, 23);
		recordPanel.add(btnAddDoctorsOrders);
		
		JButton btnDeleteDoctorsOrders = new JButton("Delete Doctors Orders");
		btnDeleteDoctorsOrders.setVisible(false);
		btnDeleteDoctorsOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TreatmentRecordPresenter presenter = new TreatmentRecordPresenter(currentPatient);
				presenter.deleteOrder();
			}
		});
		if(currentUser.getUserType().equals("Doctor")){
			btnDeleteDoctorsOrders.setVisible(true);
		}
		btnDeleteDoctorsOrders.setFont(new Font("Arial", Font.PLAIN, 10));
		btnDeleteDoctorsOrders.setBounds(432, 11, 142, 23);
		recordPanel.add(btnDeleteDoctorsOrders);
		
		JButton btnAddInvoice = new JButton("Add Invoice");
		btnAddInvoice.setFont(new Font("Arial", Font.PLAIN, 10));
		btnAddInvoice.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				InvoicePanel panel = new InvoicePanel(currentPatient.getInfo().getLastRecord(), currentPatient);
			}
		});
		btnAddInvoice.setBounds(77, 385, 133, 23);
		recordPanel.add(btnAddInvoice);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Add Treatment Record", TitledBorder.LEADING, TitledBorder.TOP, null, UIManager.getColor("Button.darkShadow")));
		panel.setBounds(10, 97, 564, 322);
		recordPanel.add(panel);
        records.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        records.setSize(600,480); 
        records.setVisible(true);
	}
}
